home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 724 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.4 KB

  1. Path: engnews1.Eng.Sun.COM!usenet
  2. From: clamage@Eng.Sun.COM (Steve Clamage)
  3. Newsgroups: comp.std.c
  4. Subject: Re: It this portable?
  5. Date: 11 Apr 1996 15:46:31 GMT
  6. Organization: Sun Microsystems Inc.
  7. Message-ID: <4kj9gn$6rm@engnews1.Eng.Sun.COM>
  8. References: <Dpp1tv.4Kq@ukpsshp1.serigate.philips.nl>
  9. Reply-To: clamage@Eng.Sun.COM
  10. NNTP-Posting-Host: taumet.eng.sun.com
  11.  
  12. In article 4Kq@ukpsshp1.serigate.philips.nl, baynes@ukpsshp1.serigate.philips.nl (Stephen Baynes) writes:
  13. >In a glossy newsletter from a company (who had better remain nameless) that 
  14. >sells tools for testing and validating C code the following bit of code was
  15. >given as an example of implementation dependent but portable code across ISO 
  16. >C implementations. I know 'portable' is not a term used in the standard, let
  17. >us assume it means conforming. Is this program conforming, giving an 
  18. >implementation defined result (regardless of the fact the result is probably
  19. >not the one intended) or is it going to give undefined behaviour?
  20. >
  21. >#include <stdio.h>
  22. >#include <limits.h>
  23. >int main( void )
  24. >{
  25. >    printf( "%d", UINT_MAX );
  26. >    return 0;
  27. >}
  28. >
  29. >My vote is for undefined, but can anyone construct an argument based on
  30. >the represention of integers that makes it implmentation defined?
  31.  
  32. The example passes an unsigned int to printf and tells printf to
  33. interpret the value as a signed int.
  34.  
  35. The Standard's section on types says that signed int and unsigned int
  36. have the same size (including sign information) and alignment, and a
  37. footnote clarifies the intention to allow interchangeability as
  38. function parameters and return values.
  39.  
  40. The section on conversions says the result of converting an unsigned int
  41. to a signed int is implementation-defined if the value of the unsigned
  42. int is not representable as a signed int.
  43.  
  44. The example has no undefined behavior, but it does have implementation-
  45. defined behavior.
  46.  
  47. I don't know what the assertion about portability means, either. The code
  48. should compile and run on any system, but need not yield the same result.
  49. I wouldn't call that portable, but to some people "portable" means "compiles
  50. without error messages".
  51.  
  52. Pragmatically, most popular machines are straight 2's complement with no
  53. overflow checking, so you will probably get the same result across a
  54. variety of compilers and platforms. Maybe that's what "portable" means:
  55. "I get the same result on the systems I currently use."
  56. ---
  57. Steve Clamage, stephen.clamage@eng.sun.com
  58.  
  59.  
  60.